home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
diskmags
/
0022-3.564
/
dmg-3413
/
news.txt
/
pascal4.asc
< prev
next >
Wrap
Text File
|
1989-04-05
|
8KB
|
216 lines
--------------------------------------------------------------
PPPPPP AAAAA SSSS S CCCCC AAAAA LL
PP PP AA AA SS SS CC C AA AA LL
PP PP AA AA SS CC AA AA LL
PPPPPP AAAAAAA SSSSS CC AAAAAAA LL
PP AA AA SS CC AA AA LL
PP AA AA SS SS CC C AA AA LL
PP AA AA S SSSS CCCCC AA AA LLLLLLL
===============================================================
TUTORIAL PART 4 - BY DEREK CLEGG
---------------------------------------------------------------
This time I'll be introducing the concept of local and global
variables, and I'll be expanding a little on parameters.
But first possible solutions to the tasks from last time;
i/ PROCEDURE Sort_Two_Ints ( X,Y : INTEGER );
(* X and Y are value parameters! *)
BEGIN
IF X > Y
THEN WRITELN ( X,Y," X,Y")
ELSE WRITELN ( Y,X," Y,X")
END;
ii/ PROGRAM Sort_Three_Ints ( INPUT, OUTPUT );
VAR N1,N2,N3, Temp : INTEGER;
PROCEDURE Sort_Two_Ints ( VAR X,Y : INTEGER );
(* X and Y are variable parameters. *)
BEGIN
IF Y < X
THEN BEGIN
Temp := X;
X := Y;
Y := Temp
END
END;
BEGIN (* Start of main program *)
READLN ( N1,N2,N3 );
Sort_Two_Ints ( N1,N2 );
Sort_Two_Ints ( N2,N3 );
Sort_Two_Ints ( N1,N2 );
WRITLEN ( N1,N2,N3," Sorted!!")
END.
iii/ PROGRAM ValidateDate ( Input, Output );
VAR Days, Month, Year : INTEGER;
Ch : CHAR;
Valid : BOOLEAN;
PROCEDURE Validate( DD,MM,YY : INTEGER; Valid : BOOLEAN );
BEGIN
IF ( DD > 31 ) OR ( DD < 1 )
THEN Valid=FALSE;
IF ( MM > 12 ) OR ( MM < 1 )
THEN Valid=FALSE;
IF ( YY > 1976 ) OR ( YY < 1901 )
THEN Valid=FALSE;
CASE MM OF
1 : IF DD <= 31 THEN Valid = TRUE
ELSE Valid = FALSE;
2 : BEGIN
IF DD > 28 THEN BEGIN
IF YY MOD 4 = 0
THEN Valid = TRUE
ELSE Valid = FALSE
END
ELSE Valid = TRUE
END;
3 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE;
4 : IF DD <=30 THEN Valid = TRUE
ELSE Valid = FALSE;
5 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE;
6 : IF DD <=30 THEN Valid = TRUE
ELSE Valid = FALSE;
7 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE;
8 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE;
9 : IF DD <=30 THEN Valid = TRUE
ELSE Valid = FALSE;
10 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE;
11 : IF DD <=30 THEN Valid = TRUE
ELSE Valid = FALSE;
12 : IF DD <=31 THEN Valid = TRUE
ELSE Valid = FALSE
END
END;
BEGIN
REPEAT
BEGIN
Valid = TRUE;
READLN( Days,Ch,Month,Ch,Year );
Validate( Days, Month, Year, Valid );
IF Valid
THEN WRITELN('This is a valid date!')
ELSE WRITELN('Sorry, Invalid date!')
ENDIF
END;
UNTIL Year=0
END.
**** I *think* the above solutions are correct, but am not sure as
they have not been checked or traced! The above solution to
the validate a date problem is very bulky and clumsy as it is
easier to solve if an array is used. When I come to discuss
arrays I may use this problem again.
If I had less overtime to do at work this would all have been
checked and wouldn't have been just thrown together but as
somebody once said, "It's a dirty job but somebody's got to do
it!"
Variable parameters
---------------------
We have already looked at value parameters, but there is another
parameter know as a variable parameter. The values of variable
parameters are preserved from one use/call to the next. Below is a
typical function heading which has two formal parameters, one value
parameter and one variable parameter.
FUNCTION Random (VAR seed : INTEGER; max : INTEGER) : INTEGER;
Note that ALL variable parameters MUST be preceeded by the keyword
VAR. Note also that there is no limit to the number of parameters a
function/procedure can have provided that when it is called, the calling
statement has the same number of actual parameters, AND these are listed
in the same order and type as the corresponding formal parameter in the
function heading.
To call the above function, you would need a pascal statement such
as ;
random_no := random( Rseed, 36 );
where 36 is the actual value parameter and Rseed is the name of the
variable ( from the calling part of the program ) that corresponds to
seed.
There is no reason why the global variable here represented by
'Rseed' should not have been called 'seed' just as it is in the function
heading except that by naming the variable parameter differently aids
readability and makes the program easier to understand though someone
who is just learning programming and has never programmed before may
get confused by the name change.
In the above example two values are passed to the function. These
are the values of Rseed and 36. When the computer has finished
executing the lines of code which make up this function, it forgets
about 36 ( ie erases 36 from its memory ) BUT the value of the LOCAL
variable 'seed' is then assigned to the GLOBAL variable 'Rseed' before
the computer erases 'seed' from its memory and the value of 'seed' also.
~~~OOOO~~~